package com.car.carcontrol.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;

import com.car.carcontrol.R;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author Sunshine
 * @Description Intent相关工具类
 */
public class Utils {

    private Utils() {
        throw new UnsupportedOperationException("u can't fuck me...");
    }

    //16进制字符串转byte[]
    public static byte[] hexStringToByteArray(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        Log.e("StringToByte===1", bytes+"==="+bytes[0]);

        return bytes;
    }

    //byte[]转16进制字符串
    public static String bytesToString(byte[] bytes) {
        final char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[bytes.length * 2];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            hexChars[i * 2] = hexArray[v >>> 4];
            hexChars[i * 2 + 1] = hexArray[v & 0x0F];

            sb.append(hexChars[i * 2]);
            sb.append(hexChars[i * 2 + 1]);
            sb.append(' ');
        }
        return sb.toString();
    }

    public static Bitmap stringtoBitmap(String string){
        //将字符串转换成Bitmap类型
        Bitmap bitmap=null;
        try {
            byte[]bitmapArray;
            bitmapArray= Base64.decode(string, Base64.DEFAULT);
            bitmap= BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap;
    }



    public static String bitmaptoString(Bitmap bitmap){
        //将Bitmap转换成字符串
        String string=null;
        ByteArrayOutputStream bStream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,bStream);
        byte[]bytes=bStream.toByteArray();
        string=Base64.encodeToString(bytes,Base64.DEFAULT);
        return string;
    }

    // 压缩
    public static String compress(String str) throws IOException {
        try {
            if (str == null || str.length() == 0) {
                return str;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
            gzip.close();
            return out.toString("ISO-8859-1");

        } catch (Exception e) {
            Log.e("utils==e", e+"===");
            e.printStackTrace();

            return "";
        }

//        return out.toString("UTF-8");
//        return out.toString("GBK");
    }

    // 解压缩
    public static String decompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
//        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("UTF-8"));
//        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("GBK"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码，也可以显式的指定如toString("GBK")
        return out.toString();
    }

//    public static final byte[] compress(String paramString) {
//        if (paramString == null)
//            return null;
//        ByteArrayOutputStream byteArrayOutputStream = null;
//        ZipOutputStream zipOutputStream = null;
//        byte[] arrayOfByte;
//        try {
//            byteArrayOutputStream = new ByteArrayOutputStream();
//            zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
//            zipOutputStream.putNextEntry(new ZipEntry("0"));
//            zipOutputStream.write(paramString.getBytes());
//            zipOutputStream.closeEntry();
//            arrayOfByte = byteArrayOutputStream.toByteArray();
//        } catch (IOException localIOException5) {
//            arrayOfByte = null;
//        } finally {
//            if (zipOutputStream != null)
//                try {
//                    zipOutputStream.close();
//                } catch (IOException localIOException6) {
//                }
//            if (byteArrayOutputStream != null)
//                try {
//                    byteArrayOutputStream.close();
//                } catch (IOException localIOException7) {
//                }
//        }
//        return arrayOfByte;
//    }
//
//    @SuppressWarnings("unused")
//    public static final String decompress(byte[] paramArrayOfByte) {
//        if (paramArrayOfByte == null)
//            return null;
//        ByteArrayOutputStream byteArrayOutputStream = null;
//        ByteArrayInputStream byteArrayInputStream = null;
//        ZipInputStream zipInputStream = null;
//        String str;
//        try {
//            byteArrayOutputStream = new ByteArrayOutputStream();
//            byteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
//            zipInputStream = new ZipInputStream(byteArrayInputStream);
//            ZipEntry localZipEntry = zipInputStream.getNextEntry();
//            byte[] arrayOfByte = new byte[1024];
//            int i = -1;
//            while ((i = zipInputStream.read(arrayOfByte)) != -1)
//                byteArrayOutputStream.write(arrayOfByte, 0, i);
//            str = byteArrayOutputStream.toString();
//        } catch (IOException localIOException7) {
//            str = null;
//        } finally {
//            if (zipInputStream != null)
//                try {
//                    zipInputStream.close();
//                } catch (IOException localIOException8) {
//                }
//            if (byteArrayInputStream != null)
//                try {
//                    byteArrayInputStream.close();
//                } catch (IOException localIOException9) {
//                }
//            if (byteArrayOutputStream != null)
//                try {
//                    byteArrayOutputStream.close();
//                } catch (IOException localIOException10) {
//                }
//        }
//        return str;
//    }

}
